<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.estimators.HillClimbSearch &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.estimators.HillClimbSearch</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">permutations</span>

<span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>

<span class="kn">from</span> <span class="nn">pgmpy.estimators</span> <span class="k">import</span> <span class="n">StructureEstimator</span><span class="p">,</span> <span class="n">K2Score</span>
<span class="kn">from</span> <span class="nn">pgmpy.models</span> <span class="k">import</span> <span class="n">BayesianModel</span>


<div class="viewcode-block" id="HillClimbSearch"><a class="viewcode-back" href="../../../estimators.html#pgmpy.estimators.HillClimbSearch.HillClimbSearch">[docs]</a><span class="k">class</span> <span class="nc">HillClimbSearch</span><span class="p">(</span><span class="n">StructureEstimator</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">scoring_method</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Class for heuristic hill climb searches for BayesianModels, to learn</span>
<span class="sd">        network structure from data. `estimate` attempts to find a model with optimal score.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data: pandas DataFrame object</span>
<span class="sd">            datafame object where each column represents one variable.</span>
<span class="sd">            (If some values in the data are missing the data cells should be set to `numpy.NaN`.</span>
<span class="sd">            Note that pandas converts each column containing `numpy.NaN`s to dtype `float`.)</span>

<span class="sd">        scoring_method: Instance of a `StructureScore`-subclass (`K2Score` is used as default)</span>
<span class="sd">            An instance of `K2Score`, `BdeuScore`, or `BicScore`.</span>
<span class="sd">            This score is optimized during structure estimation by the `estimate`-method.</span>

<span class="sd">        state_names: dict (optional)</span>
<span class="sd">            A dict indicating, for each variable, the discrete set of states (or values)</span>
<span class="sd">            that the variable can take. If unspecified, the observed values in the data set</span>
<span class="sd">            are taken to be the only possible states.</span>

<span class="sd">        complete_samples_only: bool (optional, default `True`)</span>
<span class="sd">            Specifies how to deal with missing data, if present. If set to `True` all rows</span>
<span class="sd">            that contain `np.Nan` somewhere are ignored. If `False` then, for each variable,</span>
<span class="sd">            every row where neither the variable nor its parents are `np.NaN` is used.</span>
<span class="sd">            This sets the behavior of the `state_count`-method.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">scoring_method</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">scoring_method</span> <span class="o">=</span> <span class="n">scoring_method</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">scoring_method</span> <span class="o">=</span> <span class="n">K2Score</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">HillClimbSearch</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_legal_operations</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">tabu_list</span><span class="o">=</span><span class="p">[],</span> <span class="n">max_indegree</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generates a list of legal (= not in tabu_list) graph modifications</span>
<span class="sd">        for a given model, together with their score changes. Possible graph modifications:</span>
<span class="sd">        (1) add, (2) remove, or (3) flip a single edge. For details on scoring</span>
<span class="sd">        see Koller &amp; Fridman, Probabilistic Graphical Models, Section 18.4.3.3 (page 818).</span>
<span class="sd">        If a number `max_indegree` is provided, only modifications that keep the number</span>
<span class="sd">        of parents for each node below `max_indegree` are considered.&quot;&quot;&quot;</span>

        <span class="n">local_score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoring_method</span><span class="o">.</span><span class="n">local_score</span>
        <span class="n">nodes</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_names</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
        <span class="n">potential_new_edges</span> <span class="o">=</span> <span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">permutations</span><span class="p">(</span><span class="n">nodes</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> <span class="o">-</span>
                               <span class="nb">set</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">edges</span><span class="p">())</span> <span class="o">-</span>
                               <span class="nb">set</span><span class="p">([(</span><span class="n">Y</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">)</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">edges</span><span class="p">()]))</span>

        <span class="k">for</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">)</span> <span class="ow">in</span> <span class="n">potential_new_edges</span><span class="p">:</span>  <span class="c1"># (1) add single edge</span>
            <span class="k">if</span> <span class="n">nx</span><span class="o">.</span><span class="n">is_directed_acyclic_graph</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">edges</span><span class="p">()</span> <span class="o">+</span> <span class="p">[(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">)])):</span>
                <span class="n">operation</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">operation</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tabu_list</span><span class="p">:</span>
                    <span class="n">old_parents</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_parents</span><span class="p">(</span><span class="n">Y</span><span class="p">)</span>
                    <span class="n">new_parents</span> <span class="o">=</span> <span class="n">old_parents</span> <span class="o">+</span> <span class="p">[</span><span class="n">X</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">max_indegree</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_parents</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">max_indegree</span><span class="p">:</span>
                        <span class="n">score_delta</span> <span class="o">=</span> <span class="n">local_score</span><span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">new_parents</span><span class="p">)</span> <span class="o">-</span> <span class="n">local_score</span><span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">old_parents</span><span class="p">)</span>
                        <span class="k">yield</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">score_delta</span><span class="p">)</span>

        <span class="k">for</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">)</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">edges</span><span class="p">():</span>  <span class="c1"># (2) remove single edge</span>
            <span class="n">operation</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;-&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">operation</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tabu_list</span><span class="p">:</span>
                <span class="n">old_parents</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_parents</span><span class="p">(</span><span class="n">Y</span><span class="p">)</span>
                <span class="n">new_parents</span> <span class="o">=</span> <span class="n">old_parents</span><span class="p">[:]</span>
                <span class="n">new_parents</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
                <span class="n">score_delta</span> <span class="o">=</span> <span class="n">local_score</span><span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">new_parents</span><span class="p">)</span> <span class="o">-</span> <span class="n">local_score</span><span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">old_parents</span><span class="p">)</span>
                <span class="k">yield</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">score_delta</span><span class="p">)</span>

        <span class="k">for</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">)</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">edges</span><span class="p">():</span>  <span class="c1"># (3) flip single edge</span>
            <span class="n">new_edges</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">edges</span><span class="p">()</span> <span class="o">+</span> <span class="p">[(</span><span class="n">Y</span><span class="p">,</span> <span class="n">X</span><span class="p">)]</span>
            <span class="n">new_edges</span><span class="o">.</span><span class="n">remove</span><span class="p">((</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">nx</span><span class="o">.</span><span class="n">is_directed_acyclic_graph</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">DiGraph</span><span class="p">(</span><span class="n">new_edges</span><span class="p">)):</span>
                <span class="n">operation</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;flip&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">operation</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tabu_list</span> <span class="ow">and</span> <span class="p">(</span><span class="s1">&#39;flip&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">X</span><span class="p">))</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tabu_list</span><span class="p">:</span>
                    <span class="n">old_X_parents</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_parents</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
                    <span class="n">old_Y_parents</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_parents</span><span class="p">(</span><span class="n">Y</span><span class="p">)</span>
                    <span class="n">new_X_parents</span> <span class="o">=</span> <span class="n">old_X_parents</span> <span class="o">+</span> <span class="p">[</span><span class="n">Y</span><span class="p">]</span>
                    <span class="n">new_Y_parents</span> <span class="o">=</span> <span class="n">old_Y_parents</span><span class="p">[:]</span>
                    <span class="n">new_Y_parents</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
                    <span class="k">if</span> <span class="n">max_indegree</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_X_parents</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">max_indegree</span><span class="p">:</span>
                        <span class="n">score_delta</span> <span class="o">=</span> <span class="p">(</span><span class="n">local_score</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">new_X_parents</span><span class="p">)</span> <span class="o">+</span>
                                       <span class="n">local_score</span><span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">new_Y_parents</span><span class="p">)</span> <span class="o">-</span>
                                       <span class="n">local_score</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">old_X_parents</span><span class="p">)</span> <span class="o">-</span>
                                       <span class="n">local_score</span><span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">old_Y_parents</span><span class="p">))</span>
                        <span class="k">yield</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">score_delta</span><span class="p">)</span>

<div class="viewcode-block" id="HillClimbSearch.estimate"><a class="viewcode-back" href="../../../estimators.html#pgmpy.estimators.HillClimbSearch.HillClimbSearch.estimate">[docs]</a>    <span class="k">def</span> <span class="nf">estimate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tabu_length</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">max_indegree</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Performs local hill climb search to estimates the `BayesianModel` structure</span>
<span class="sd">        that has optimal score, according to the scoring method supplied in the constructor.</span>
<span class="sd">        Starts at model `start` and proceeds by step-by-step network modifications</span>
<span class="sd">        until a local maximum is reached. Only estimates network structure, no parametrization.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        start: BayesianModel instance</span>
<span class="sd">            The starting point for the local search. By default a completely disconnected network is used.</span>
<span class="sd">        tabu_length: int</span>
<span class="sd">            If provided, the last `tabu_length` graph modifications cannot be reversed</span>
<span class="sd">            during the search procedure. This serves to enforce a wider exploration</span>
<span class="sd">            of the search space. Default value: 100.</span>
<span class="sd">        max_indegree: int or None</span>
<span class="sd">            If provided and unequal None, the procedure only searches among models</span>
<span class="sd">            where all nodes have at most `max_indegree` parents. Defaults to None.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        model: `BayesianModel` instance</span>
<span class="sd">            A `BayesianModel` at a (local) score maximum.</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; import pandas as pd</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.estimators import HillClimbSearch, BicScore</span>
<span class="sd">        &gt;&gt;&gt; # create data sample with 9 random variables:</span>
<span class="sd">        ... data = pd.DataFrame(np.random.randint(0, 5, size=(5000, 9)), columns=list(&#39;ABCDEFGHI&#39;))</span>
<span class="sd">        &gt;&gt;&gt; # add 10th dependent variable</span>
<span class="sd">        ... data[&#39;J&#39;] = data[&#39;A&#39;] * data[&#39;B&#39;]</span>
<span class="sd">        &gt;&gt;&gt; est = HillClimbSearch(data, scoring_method=BicScore(data))</span>
<span class="sd">        &gt;&gt;&gt; best_model = est.estimate()</span>
<span class="sd">        &gt;&gt;&gt; sorted(best_model.nodes())</span>
<span class="sd">        [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;]</span>
<span class="sd">        &gt;&gt;&gt; best_model.edges()</span>
<span class="sd">        [(&#39;B&#39;, &#39;J&#39;), (&#39;A&#39;, &#39;J&#39;)]</span>
<span class="sd">        &gt;&gt;&gt; # search a model with restriction on the number of parents:</span>
<span class="sd">        &gt;&gt;&gt; est.estimate(max_indegree=1).edges()</span>
<span class="sd">        [(&#39;J&#39;, &#39;A&#39;), (&#39;B&#39;, &#39;J&#39;)]</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">epsilon</span> <span class="o">=</span> <span class="mi">1</span><span class="n">e</span><span class="o">-</span><span class="mi">8</span>
        <span class="n">nodes</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">state_names</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">start</span> <span class="o">=</span> <span class="n">BayesianModel</span><span class="p">()</span>
            <span class="n">start</span><span class="o">.</span><span class="n">add_nodes_from</span><span class="p">(</span><span class="n">nodes</span><span class="p">)</span>
        <span class="k">elif</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">BayesianModel</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">set</span><span class="p">(</span><span class="n">start</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span> <span class="o">==</span> <span class="nb">set</span><span class="p">(</span><span class="n">nodes</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;&#39;start&#39; should be a BayesianModel with the same variables as the data set, or &#39;None&#39;.&quot;</span><span class="p">)</span>

        <span class="n">tabu_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">current_model</span> <span class="o">=</span> <span class="n">start</span>

        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">best_score_delta</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">best_operation</span> <span class="o">=</span> <span class="kc">None</span>

            <span class="k">for</span> <span class="n">operation</span><span class="p">,</span> <span class="n">score_delta</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_legal_operations</span><span class="p">(</span><span class="n">current_model</span><span class="p">,</span> <span class="n">tabu_list</span><span class="p">,</span> <span class="n">max_indegree</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">score_delta</span> <span class="o">&gt;</span> <span class="n">best_score_delta</span><span class="p">:</span>
                    <span class="n">best_operation</span> <span class="o">=</span> <span class="n">operation</span>
                    <span class="n">best_score_delta</span> <span class="o">=</span> <span class="n">score_delta</span>

            <span class="k">if</span> <span class="n">best_operation</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">best_score_delta</span> <span class="o">&lt;</span> <span class="n">epsilon</span><span class="p">:</span>
                <span class="k">break</span>
            <span class="k">elif</span> <span class="n">best_operation</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;+&#39;</span><span class="p">:</span>
                <span class="n">current_model</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="o">*</span><span class="n">best_operation</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">tabu_list</span> <span class="o">=</span> <span class="p">([(</span><span class="s1">&#39;-&#39;</span><span class="p">,</span> <span class="n">best_operation</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span> <span class="o">+</span> <span class="n">tabu_list</span><span class="p">)[:</span><span class="n">tabu_length</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">best_operation</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;-&#39;</span><span class="p">:</span>
                <span class="n">current_model</span><span class="o">.</span><span class="n">remove_edge</span><span class="p">(</span><span class="o">*</span><span class="n">best_operation</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">tabu_list</span> <span class="o">=</span> <span class="p">([(</span><span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="n">best_operation</span><span class="p">[</span><span class="mi">1</span><span class="p">])]</span> <span class="o">+</span> <span class="n">tabu_list</span><span class="p">)[:</span><span class="n">tabu_length</span><span class="p">]</span>
            <span class="k">elif</span> <span class="n">best_operation</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;flip&#39;</span><span class="p">:</span>
                <span class="n">X</span><span class="p">,</span> <span class="n">Y</span> <span class="o">=</span> <span class="n">best_operation</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">current_model</span><span class="o">.</span><span class="n">remove_edge</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">)</span>
                <span class="n">current_model</span><span class="o">.</span><span class="n">add_edge</span><span class="p">(</span><span class="n">Y</span><span class="p">,</span> <span class="n">X</span><span class="p">)</span>
                <span class="n">tabu_list</span> <span class="o">=</span> <span class="p">([</span><span class="n">best_operation</span><span class="p">]</span> <span class="o">+</span> <span class="n">tabu_list</span><span class="p">)[:</span><span class="n">tabu_length</span><span class="p">]</span>

        <span class="k">return</span> <span class="n">current_model</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>